//
//  SignatureViewController.m
//  CarLife
//
//  Created by yu on 2018/3/27.
//  Copyright © 2018年 jpas. All rights reserved.
//

#import "SignatureViewController.h"
#import "BJTSignView.h"

@interface SignatureViewController ()

@property (nonatomic, strong) BJTSignView *signView;
@property (nonatomic, strong) UIButton *confirmBtn;
@property (nonatomic, strong) UIButton *resignBtn;
@property (nonatomic, strong) UILabel *tipLabel;
@property (nonatomic, strong) UIButton *cancelBtn;

@end

@implementation SignatureViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = kBackGroundLightGrayColor;
    
    // 显示状态栏
//    [UIApplication sharedApplication].statusBarHidden = NO;
//    // 这个方向根据需求调整方向
//    [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight];
    
    UIButton *cancelBtn = [[UIButton alloc] init];
    [cancelBtn setTitleColor:kTextBlackColor forState:UIControlStateNormal];
    [cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
    [cancelBtn addTarget:self action:@selector(cancelAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:cancelBtn];
    self.cancelBtn = cancelBtn;
    
    /////确定
    self.confirmBtn = [[UIButton alloc] init];
    [self.confirmBtn setTitleColor:kTextBlackColor forState:UIControlStateNormal];
    [self.confirmBtn setTitle:@"确定" forState:UIControlStateNormal];
    [self.confirmBtn addTarget:self action:@selector(confirmAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.confirmBtn];
    
    self.signView = [[BJTSignView alloc] init];
    self.signView.backgroundColor = [UIColor whiteColor];
    self.signView.layer.borderColor = [UIColor blackColor].CGColor;
    self.signView.layer.borderWidth = 1;
    [self.view addSubview:self.signView];
    
    self.resignBtn = [[UIButton alloc] init];
    [self.resignBtn setTitleColor:kTextBlackColor forState:UIControlStateNormal];
    [self.resignBtn setTitle:@"重签" forState:UIControlStateNormal];
    [self.resignBtn addTarget:self action:@selector(resignAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.resignBtn];
    
    
    self.tipLabel = [[UILabel alloc] init];
    self.tipLabel.text = @"注：请保持字迹工整";
    self.tipLabel.font = [UIFont systemFontOfSize:15];
    self.tipLabel.textColor = [UIColor redColor];
    [self.view addSubview:self.tipLabel];
    
    CGFloat margin10 = 10.0;CGFloat margin15 = 15.0;
    {
        cancelBtn.sd_layout
        .leftSpaceToView(self.view, margin10)
        .topSpaceToView(self.view, margin10/2)
        .widthIs(60)
        .heightIs(45);
        
        self.confirmBtn.sd_layout
        .centerYEqualToView(self.cancelBtn)
        .rightSpaceToView(self.view, margin10)
        .widthRatioToView(self.cancelBtn, 1)
        .heightRatioToView(self.cancelBtn, 1);
        
        self.resignBtn.sd_layout
        .bottomSpaceToView(self.view, margin10/2)
        .rightEqualToView(self.confirmBtn)
        .widthRatioToView(self.confirmBtn, 1)
        .heightRatioToView(self.confirmBtn, 1);
    }
    
    self.tipLabel.sd_layout
    .leftSpaceToView(self.view, margin15)
    .rightSpaceToView(self.resignBtn, margin15)
    .centerYEqualToView(self.resignBtn)
    .heightIs(25);
    
    self.signView.sd_layout
    .leftSpaceToView(self.view, margin15)
    .topSpaceToView(self.cancelBtn, 0)
    .rightSpaceToView(self.view, margin15)
    .bottomSpaceToView(self.resignBtn, 0);
}

- (void)cancelAction {
    self.cancelBlock();
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)confirmAction {
    UIImage *image = [self.signView getSignatureImage];
    if (image) {
        self.confirmBlock(image);
        [self dismissViewControllerAnimated:YES completion:nil];
    } else {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"请签名" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
        [alert show];
    }
}

- (void)resignAction {
    [self.signView clearSignature];
}

/////////
//支持旋转
- (BOOL)shouldAutorotate{
    return YES;
}

//支持的方向
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskLandscapeRight;
}

//- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
//    return UIInterfaceOrientationLandscapeRight;
//}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
